import { DataBase, DataStructure } from "./database";
import { DeviceList } from "./deviceList";


enum LOG_TYPE {
    LED_TEXT,
}

export enum LedTaskExecutedStatus {
    Wait = 0,
    Success = 1,
    CreateTaskSuccess = 2,
}

/** LED 任务 */
export class LedTask extends DataBase {
    public static collectionName: string = "LedTask";

    public logType: LOG_TYPE;
    public deviceId: string;
    public deviceListId: string;
    public changeTime: Date;
    public ledtext: string;
    public speed: number;
    public count: number;
    public groupId: string;
    public taskId: string;

    public executedStatus: LedTaskExecutedStatus;

    constructor(data: DataStructure = {}) {
        super(data);
        this.logType = data['logType']
        this.deviceId = data['deviceId'] || ''
        this.groupId = data['groupId'] || ''
        this.deviceListId = data['deviceListId'] || ''
        this.ledtext = data['ledtext'] || ''
        this.taskId = data['taskId'] || ''
        this.speed = data['speed'] || 50
        this.count = data['count'] || 0
        this.executedStatus = data['executedStatus']
        this.changeTime = new Date(data['changeTime'])
    }

    /**
     * 创建任务
     * @param deviceList 设备信息
     * @param deviceId 设备Id
     * @param speed 速度
     * @param count 次数
     */
    public static async CreateLedTaskText(deviceList: DeviceList, ledtext: string, speed: number, count: number, changeTime: Date | string | Number) {
        const a = await this.AddData(new LedTask({
            deviceId: deviceList.info.deviceId,
            groupId: deviceList.groupId,
            deviceListId: deviceList.get_id(),
            ledtext,
            speed,
            count,
            executedStatus: LedTaskExecutedStatus.Wait,
            changeTime,
        }))
        return a
    }

    public static async GetLedTask(deviceList: DeviceList, offset: number = 0,) {
        const orderBy: OrderByArray = {
            fieldPath: 'otherInformation.createDate',
            string: 'desc',
        }
        const a = await this.GetData({
            groupId: deviceList.groupId,
            deviceListId: deviceList.get_id(),
        }, offset, {}, [orderBy])
        return a.map(x => new LedTask(x))
    }

    public async setExecutedStatus(executedStatus: LedTaskExecutedStatus) {
        this.executedStatus = executedStatus
        return await LedTask.UpdateDataInWhere2({
            executedStatus,
        }, {
            groupId: this.groupId,
            deviceListId: this.deviceListId,
        })
    }
}